.PHONY: help build

# Target Docker Registry
REGISTRY_HOST?=
REGISTRY_USERNAME?=
REGISTRY_PASSWORD?=
DOCKER_HUB_HOST:=registry.cn-hangzhou.aliyuncs.com

# Define the docker repository and component part of the tag
REPO:=xiaokang
NAME:=ruby_env
IMAGE:=$(DOCKER_HUB_HOST)/$(REPO)/$(NAME)
VERSION:=$(shell cat VERSION)

# Terminal coloring
RED:=$$(tput setaf 1)
GREEN:=$$(tput setaf 2)
BLUE:=$$(tput setaf 4)
CLEAR:=$$(tput sgr 0)
BOLD:=$$(tput bold)

## Global commands
help:
	@echo "$(BOLD)Name:$(NAME)"
	@echo
	@echo "$(BOLD)Global commands:$(CLEAR)"
	@echo "  $(BOLD)$(GREEN)help$(CLEAR)    Display this menu."
	@echo
	@echo "$(BOLD)Workstation commands:$(CLEAR)"
	@echo "  $(BOLD)$(GREEN)build$(CLEAR)      Build all tool images."
	@echo

build:
	@echo "$(BOLD)$(GREEN)Building $(IMAGE):$(VERSION) image...$(CLEAR)"
	@docker build -t $(IMAGE):$(VERSION) .
	@echo "$(BOLD)$(GREEN)Image $(IMAGE) was built.$(CLEAR)"

_remove-dangling-images:
	@echo "$(BOLD)$(GREEN)Cleaning up dangling images...$(CLEAR)"
	@-\
		IMAGES=$$(docker images --filter="dangling=true" --quiet) && \
		if [ -n "$$IMAGES" ]; then \
			docker rmi $$IMAGES; \
		else \
			echo "No dangling images found, skipping."; \
		fi
